//
//  MineRequest.m
//  Cycling
//
//  Created by 迟国强 on 2023/3/28.
//

#import "MineRequest.h"
#import "BaseBodyModel.h"
#import "UerCenterModel.h"

@implementation MineRequest

+ (void)getUserInfoMessageWithEid:(NSString *)eid result:(void(^)(UerCenterModel *model))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_UserCenter];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:eid forKey:@"eid"];
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                if (model.code == 1) {
                    NSDictionary *dic = model.data;
                    UerCenterModel *userModel = [UerCenterModel mj_objectWithKeyValues:dic];
                    resultBlock(userModel);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
            
        }
    }];
}
/// 更新图片接口
+ (void)uploadImageWithFile:(NSData *)file type:(NSString *)type result:(void(^)(NSDictionary *ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Mine_API_UploadImage];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    if(type.length) {
        [parm setObject:type forKey:@"type"];
    }
    [self basePOST:url cache:YES autoHideLoading:NO parms:parm data:file result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
//                UerCenterModel *userModel = [UerCenterModel mj_objectWithKeyValues:dic];
                if (model.code == 1) {
                    resultBlock(dic);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
            
        }
    }];
}
/// 更新车辆图片
+ (void)uploadCarImageWithFile:(NSString *)file carId:(NSString *)carId result:(void(^)(BOOL ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Mine_API_UploadCarImage];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:carId forKey:@"id"];
    [parm setObject:file forKey:@"img"];
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(NO);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                if (model.code == 1) {
                    resultBlock(YES);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(NO);
                }
            }
        }
    }];
}

/// 更新头像接口
+ (void)uploadavatarWithFile:(NSData *)file type:(NSString *)type result:(void(^)(NSDictionary *ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Mine_API_Uploadavatar];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    if(type.length) {
        [parm setObject:type forKey:@"type"];
    }
    [self basePOST:url cache:YES autoHideLoading:NO parms:parm data:file result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
//                UerCenterModel *userModel = [UerCenterModel mj_objectWithKeyValues:dic];
                if (model.code == 1) {
                    resultBlock(dic);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
            
        }
    }];
}

+ (void)getUserDevices:(NSInteger)page result:(void(^)(NSArray<DeviceModel *> *list))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl, Cyc_API_UserDevices];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:@(page) forKey:@"page"];
    [parm setObject:@10 forKey:@"pageSize"];

    [self baseGET:url cache:NO autoHideLoading:NO parms:parm request:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
                if (model.code == 1) {
                    NSArray *list = dic[@"list"];
                    NSMutableArray *ary = [NSMutableArray arrayWithCapacity:1];
                    for (NSDictionary *dicmodel in list) {
                        DeviceModel *device = [DeviceModel modelWithJSON:dicmodel];
                        [ary addObject:device];
                    }
                    resultBlock([ary copy]);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
            
        }
    }];

}

+ (void)getApplyTryrideResult:(void(^)(NSDictionary *ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl, Cyc_API_ApplyTryride];
    [self baseGET:url cache:NO autoHideLoading:NO parms:nil request:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
//                UerCenterModel *userModel = [UerCenterModel mj_objectWithKeyValues:dic];
                if (model.code == 1) {
                    resultBlock(dic);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
            
        }
    }];

}

+ (void)refuseRideWithId:(NSString *)Id Result:(void(^)(BOOL ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl, Cyc_API_RefusedTryride];
    [self baseGET:url cache:NO autoHideLoading:NO parms:@{@"id":Id} request:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
                if (model.code == 1) {
                    resultBlock(dic);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
            
        }
    }];
}
+ (void)endRideWithId:(NSString *)Id Result:(void(^)(BOOL ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl, Cyc_API_EndTryride];
    [self baseGET:url cache:NO autoHideLoading:NO parms:@{@"id":Id} request:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
                if (model.code == 1) {
                    resultBlock(dic);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
            
        }
    }];
}
+ (void)acceptRideWithId:(NSString *)Id Result:(void(^)(BOOL ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl, Cyc_API_AcceptTryride];
    [self baseGET:url cache:NO autoHideLoading:NO parms:@{@"id":Id} request:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(NO);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
                if (model.code == 1) {
                    resultBlock(YES);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(NO);
                }
            }
        }
    }];
}

+ (void)commentTryrideWithId:(NSString *)Id content:(NSString *)content Result:(void(^)(BOOL ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl, Cyc_API_CommentTryride];
    [self baseGET:url cache:NO autoHideLoading:NO parms:@{@"id":Id,@"str":content} request:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(NO);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
                if (model.code == 1) {
                    resultBlock(YES);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(NO);
                }
            }
            
        }
    }];
}
/// 修改资料
+ (void)changeUserInfoWithTryride:(NSString *)tryride Result:(void(^)(BOOL ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_CompleFile];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:tryride forKey:@"tryride"];

    [self basePOST:url cache:YES autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(NO);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];

                if (model.code == 1) {
                    resultBlock(YES);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(NO);
                }
            }
            
        }
    }];
}

// 修改用户资料，是否有车
+ (void)changeUserInfoWithHaveBike:(BOOL)haveBike result:(void(^)(BOOL ret))resultBlock
{
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_CompleFile];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:@(haveBike ? 1 : 0) forKey:@"bikestatus"];

    [self basePOST:url cache:YES autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(NO);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];

                if (model.code == 1) {
                    resultBlock(YES);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(NO);
                }
            }
            
        }
    }];
}

// 发送设备pushToken
+ (void)postDevicePushTokenWithTokenString:(NSString *)tokenString
{
    if(tokenString.length == 0) {
        return;
    }
    NSString *url = [NSString stringWithFormat:@"%@%@", BaseUrl, Cyc_API_UploadPushToken];
    
    [self basePOST:url cache:NO autoHideLoading:NO parms:@{@"apple_token" : tokenString} result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultObject != nil) {
            BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
            if(model.code == 1) {
                //成功了
            }
        }
    }];
}

@end
